<template>
	<view >
		<uni-section title="企业信息完善" type="line"></uni-section>
		<view class="p15">
			<view class="p15-0 text-center">
				<text class="text">企业logo</text>
				<div class="upload-logo">
					<uni-file-picker v-model="logo" @select="onUpload" return-type="object" :del-icon="false" disable-preview file-mediatype="image">选择</uni-file-picker>
				</div>
			</view>
			<uni-forms label-position="top" ref="baseForm" :modelValue="form" label-width="120">
				<uni-forms-item label="名称:" required>
					<uni-easyinput v-model="form.full_name" placeholder="请输入企业名称" />
				</uni-forms-item>
				<uni-forms-item label="规模:">
					<uni-data-select v-model="form.scale" :localdata="range"></uni-data-select>
				</uni-forms-item>
				<uni-forms-item label="地区:">
					<picker @change="onRegionChange" :value="form.region" mode="region">
						<slot>
							<div class="region">
								{{form.province}} - {{form.city}} - {{form.areas}}
							</div>
						</slot>
					</picker>
				</uni-forms-item>
				<uni-forms-item label="地址:">
					<uni-easyinput v-model="form.address" placeholder="请输入企业地址" />
				</uni-forms-item>
				<uni-forms-item label="介绍:">
					<uni-easyinput type="textarea" v-model="form.introduce" placeholder="请输入介绍" />
				</uni-forms-item>
				<uni-forms-item label="介绍图:">
					<view class="example-body">
						<uni-file-picker limit="9" title="最多选择9张图片" @select="onSelectImage" @delete="onDeleteImage" :value="form.fileLists"></uni-file-picker>
					</view>
				</uni-forms-item>
				<uni-forms-item label="企业福利:">
					<uni-data-checkbox v-model="form.welfares" multiple :localdata="welfares" />
				</uni-forms-item>
				<uni-forms-item label="法定代表人:">
					<uni-easyinput v-model="form.legal_person" placeholder="请输入法定代表人" />
				</uni-forms-item>
				<uni-forms-item label="注册资本:">
					<uni-easyinput v-model="form.registered_capital" placeholder="请输入注册资本" />
				</uni-forms-item>
				<uni-forms-item label="成立日期:">
					<uni-datetime-picker type="date" return-type="timestamp" v-model="form.registered_date"/>
				</uni-forms-item>
				<uni-forms-item label="经营范围:">
					<uni-easyinput type="textarea" v-model="form.licence" placeholder="请输入经营范围" />
				</uni-forms-item>
			</uni-forms>
			<button class="default-button" @click="save()">提交</button>
		</view>
		
		<uni-popup ref="alertDialog" type="dialog">
			<uni-popup-dialog type="success" cancelText="取消" confirmText="去发布" title="通知" content="保存成功,是否去发布职位" @confirm="confirmPush"></uni-popup-dialog>
		</uni-popup>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				form:{
					log:'https://xinzhu-1301398555.cos.ap-shanghai.myqcloud.com/wxapp/company-logo.png',
					full_name:'',
					scale:0,
					province:'请选择',
					city:'请选择',
					areas:'请选择',
					address:'',
					introduce:'',
					welfares:[],
					region:[],
					legal_person:'',
					registered_capital:'',
					registered_date:'',
					licence:'',
					fileLists:[]
				},
				logo:{name:"company-logo.png",extname:"png",url:"https://xinzhu-1301398555.cos.ap-shanghai.myqcloud.com/wxapp/company-logo.png"},
				welfares:[],
				range:[],
				array:[]
			}
		},
		onShow() {
			this.range = this.$func.map_arr(this.$func.scale)
		},
		onLoad() {
			this.onGetData()
		},
		methods: {
			save(){
				let _this = this
				_this.$refs.alertDialog.open()
				if(!this.form.full_name){
					uni.showToast({title: '请输入企业名称',icon: 'error',duration: 2000})
					return;
				}
				
				let img = []
				_this.form.fileLists.forEach(function(element) {
					if(element.url){
						img.push(element.url)
					}
				});
				_this.form.images = JSON.stringify(img)
				if(_this.form.registered_date){
					_this.form.registered_date =  _this.form.registered_date / 1000
				}
				this.form.welfare = JSON.stringify(this.form.welfares)
				this.$request_api('applet_api/recruit_company','POST',this.form).then(res => {
					if(res.code == 200){
						
					}
				}).catch(err => {
					console.log(err)
				})
			},
			onGetData(){
				let _this = this
				this.$request_api('applet_api/recruit_company_info','POST',{}).then(res => {
					if(res.data.id){
						_this.form = res.data
						_this.form.scale_name = _this.$func.scale[res.data.scale]
						_this.form.welfares = res.data.welfare ? JSON.parse(res.data.welfare) : [],
						_this.form.registered_date =  _this.form.registered_date * 1000
						let img = JSON.parse(_this.form.images)
						_this.form.fileLists = []
						if(img.length){
							_this.form.fileLists = img.map(function(data){
								let obj = {url:data,name:data,extname:'png'}
								return obj
							})
						}
					}
					
					_this.welfares = _this.$func.map_uniapp_arr(res.welfare)
				}).catch(err => {
					console.log(err)
				})
			},
			confirmPush(e){
				let _this = this
				uni.navigateTo({
					url: '/user/recruit/recruit'
				});
			},
			onRegionChange(e){
				let _this = this
				_this.form.province = e.detail.value[0],
				_this.form.city = e.detail.value[1],
				_this.form.areas = e.detail.value[2]
			},
			onSelectImage(chooseImageRes){
				let _this = this
				const tempFilePaths = chooseImageRes.tempFilePaths;
				const api_url = _this.$func.api
				tempFilePaths.forEach((e) => {
					let fileurl = _this.$func.imgToBase64(e, base64 => {
						if (base64) {
							uni.uploadFile({
								url: api_url+'upload/WxAppOneLoad', //仅为示例，非真实的接口地址
								filePath: e,
								name: 'file',
								header:{
									'Content-Type':'multipart/form-data',
								},
								formData: {
									key: 'user',
									fileurl: base64,
									name: 'file',
								},
								success: (uploadFileRes) => {
									uploadFileRes.data = JSON.parse(uploadFileRes.data)
									let obj = {url:uploadFileRes.data.data,name:e,extname:'png'}
									_this.form.fileLists.push(obj)
								}
							});
						} else {
							console.error('转换失败');
						}
					});
				})
				
			},
			onDeleteImage(e){
				let arr = []
				this.form.fileLists.forEach(function(element) {
					if(element.name != e.tempFilePath){
						arr.push(element)
					}
				});
				this.form.fileLists = arr
			},
			imgToBase64(imagePath,type) {
				let _this = this
				const fileSystemManager = uni.getFileSystemManager();
				fileSystemManager.readFile({
					filePath: imagePath,
					encoding: 'base64',
					success: res => {
							
						if(type == 'avatar'){
							let fileurl = res.data
							_this.onUploadOneImg(fileurl)
							//执行上传方法
							return;
						}
						_this.images.push({name:imagePath,path:res.data})
					},
					fail: err => {
						callback(null);
					}
				});
			},
			onUpload(chooseImageRes){
				let _this = this
				const tempFilePaths = chooseImageRes.tempFilePaths;
				const api_url = _this.$func.api
				let fileurl = _this.$func.imgToBase64(tempFilePaths[0], base64 => {
								if (base64) {
									uni.uploadFile({
										url: api_url+'upload/WxAppOneLoad', //仅为示例，非真实的接口地址
										filePath: tempFilePaths[0],
										name: 'file',
										header:{
											'Content-Type':'multipart/form-data',
										},
										formData: {
											key: 'org',
											fileurl: base64,
											name: 'file',
										},
										success: (uploadFileRes) => {
											uploadFileRes.data = JSON.parse(uploadFileRes.data)
											_this.form.log = uploadFileRes.data.data
											console.log(uploadFileRes);
										}
									});
								} else {
									console.error('转换失败');
								}
							});
			}
			// api/applet_api/recruit_company
		}
	}
</script>

<style>
.region{border: 1px solid #e5e5e5;font-size: 12px;height: 35px;line-height: 35px;padding-left: 8px;color: #6a6a6a;}
.upload-logo .uni-file-picker{margin-left: 33.33%;margin-top: 15px;}
</style>
